package user.zc.excel.render;


import user.zc.excel.WriteDTO;
import user.zc.excel.data.info.ICell;
import user.zc.excel.data.wrap.ExcelDataStart;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.ArrayList;
import java.util.List;

public class ExcelRender<T> implements ICreate<XSSFWorkbook,XSSFWorkbook> {


    private List<T> list;
    private String name;
    private List<ICell.CellInfo[]> all;

    private int freeRows = 0;
    public ExcelRender (List<T> list,String name){
        this.list = list;
        this.name = name;
    }

    @Override
    public XSSFWorkbook next() {
        XSSFWorkbook workbook = new XSSFWorkbook();
        WriteDTO writeDTO = ExcelDataStart.start(list);
        all = new ArrayList<>();
        all.add(writeDTO.getColumns());
        all.add(writeDTO.getTitles());
        all.add(writeDTO.getMerged());
        all.add(writeDTO.getColumns());
        all.addAll(writeDTO.getCells());
        if(writeDTO.getTitles() != null){
            freeRows++;
        }
        if(writeDTO.getMerged() != null){
            freeRows++;
        }
        if(writeDTO.getColumns() != null){
            freeRows++;
        }
        draw(workbook);
        return workbook;
    }

    @Override
    public void draw(XSSFWorkbook workbook) {
        new SheetRender(workbook,name,freeRows).draw(all);
    }
}
